package com.example.originbot;

import com.google.protobuf.ByteString;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

public abstract class TCPClient {

    private byte[] buffer = new byte[1024 * 1024];
    private int buffer_index = 0;
    private OutputStream outputStream;
    private InputStream inputStream;
    private boolean canSend = true;
    ArrayBlockingQueue<ByteString> queue = new ArrayBlockingQueue(100);

    public abstract void processPacket(byte[] packet);

    private void dealData(byte data) {
        if (buffer_index < buffer.length - 1) {
            buffer[buffer_index] = data;
            buffer_index++;
            if (buffer_index >= kDelimiter.length) {
                byte comp[] = new byte[kDelimiter.length];
                System.arraycopy(buffer, buffer_index - comp.length, comp, 0, comp.length);
                if (Arrays.equals(comp, kDelimiter)) {
                    byte packet[] = new byte[buffer_index - kDelimiter.length];
                    System.arraycopy(buffer, 0, packet, 0, packet.length);
                    processPacket(packet);
                    buffer_index = 0;
                }
            }
        } else {
            buffer_index = 0;
        }
    }

    public void sendData(ByteString data) {
        if (!canSend) {
            return;
        }
        if (outputStream != null) {
            try {
                queue.add(data);
            } catch (Exception e) {

            }
        }
    }

    public boolean sendData() {
        if (!canSend) {
            return false;
        }
        if (outputStream != null) {
            try {
                ByteString data = queue.take();
                outputStream.write(data.toByteArray());
                return true;
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean sendData(byte data[]) {
        if (!canSend) {
            return false;
        }
        if (outputStream != null) {
            try {
                outputStream.write(data);
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    private void sendSendSwitch(boolean canSend) {
        this.canSend = canSend;
    }

    Socket socket = null;

    public void linkServer(String serverAddress, int port) throws IOException {
        // 创建套接字连接
        socket = new Socket(serverAddress, port);
        // 获取输入流
        inputStream = socket.getInputStream();
        outputStream = socket.getOutputStream();
    }

    public void beginReceive() {
        byte data[] = new byte[102400];
        while (inputStream != null) {
            int n;
            try {
                n = inputStream.read(data);
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
            if (n <= 0) {
                System.out.println("read over");
                break;
            }
            for (int i = 0; i < n; i++) {
                dealData(data[i]);
            }
        }
        stop();
    }

    public boolean isAvaliable() {
        return socket != null && inputStream != null && outputStream != null;
    }

    public void stop() {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            inputStream = null;
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            outputStream = null;
        }
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            socket = null;
        }
    }

    public static byte[] kDelimiter = "$$panxuesen$$".getBytes(); // 分隔符
}
